import datetime
import time
import logging
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException, WebDriverException
from dbutils import DBUtils

from selenium.webdriver.edge.options import Options  # 新增导入

# 修改浏览器初始化部分
# edge_options = Options()
# 设置用户数据目录（需要替换为你的实际路径）
# user_data_dir = r"C:\Users\relax\AppData\Local\Microsoft\Edge\User Data"
# edge_options.add_argument(f"--user-data-dir={user_data_dir}")
# 使用默认配置文件（根据实际情况可能需要调整）
# edge_options.add_argument("--profile-directory=Default")



# 配置日志
logging.basicConfig(level=logging.ERROR,
                    format='%(asctime)s - %(levelname)s - %(message)s')
# 替换原来的 browser = webdriver.Edge()
browser = webdriver.Edge()  # 修改后的初始化方式
index_url = 'https://www.zhipin.com/web/geek/job?city=101190400&position=100106,100107&page='
today = datetime.date.today().strftime('%Y-%m-%d')

def safe_get_text(element, xpath, default=""):
    """安全获取元素文本"""
    try:
        return element.find_element(By.XPATH, xpath).text.strip()
    except NoSuchElementException:
        logging.warning(f"元素未找到: {xpath}")
        return default

def process_page(page_num):
    try:
        browser.get(f"{index_url}{page_num}")
        time.sleep(7)  # 保留原有等待逻辑
        
        job_list = browser.find_elements(By.XPATH, '//li[contains(@class, "job-card-wrapper")]')
        if not job_list:
            logging.warning(f"第 {page_num} 页没有找到职位列表")
            return False

        for job in job_list:
            try:
                handle_main = browser.current_window_handle
                current_a = job.find_element(By.XPATH, './/a')
                browser.execute_script("arguments[0].scrollIntoView({block: 'center'});", current_a)
                time.sleep(10)
                current_a.click()
                time.sleep(10)

                # 处理窗口切换
                if len(browser.window_handles) < 2:
                    logging.warning("新窗口打开失败")
                    continue
                    
                browser.switch_to.window(browser.window_handles[-1])

                # 使用统一的异常处理
                job_data = {
                    'job_loc': safe_get_text(browser, '//div[contains(@class, "location-address")]'),
                    'job_sec': safe_get_text(browser, '//div[contains(@class,"job-sec-text")]'),
                    'job_title': safe_get_text(browser, '//div[contains(@class,"name")]/h1'),
                    'job_company': safe_get_text(browser, '//li[contains(@class,"company-name")]'),
                    'job_experience': safe_get_text(browser, '//span[contains(@class,"text-desc text-experiece")]'),
                    'job_education': safe_get_text(browser, '//span[contains(@class,"text-desc text-degree")]'),
                    'job_industry': safe_get_text(browser, '//p/a[@ka="job-detail-brandindustry"]'),
                    'job_scale': safe_get_text(browser, '(//div[contains(@class,"sider-company")]/p)[2]'),
                    'job_salary_range': safe_get_text(browser, '//div[contains(@class, "info-primary")]//span[contains(@class, "salary")]'),
                    'job_skills': ','.join([skill.text.strip() for skill in 
                                         browser.find_elements(By.XPATH, '//ul[contains(@class, "job-keyword-list")]/li')])
                }

                # 必填字段校验
                if not job_data['job_title']:
                    logging.warning("缺失关键字段，跳过当前职位")
                    continue

                print(job_data)
                # 数据库操作
                try:
                    db = DBUtils('localhost', 'root', '123456', 'spider_db')
                    db.insert_data(
                        """INSERT INTO job_info(
                            job_loc, job_sec, job_title, job_company, job_experience, 
                            job_education, job_industry, job_scale, 
                            job_salary_range, job_skills, create_time
                        ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""",  # 修正SQL语法
                        args=(job_data['job_loc'], job_data['job_sec'], job_data['job_title'],
                                job_data['job_company'],
                             job_data['job_experience'], job_data['job_education'],
                             job_data['job_industry'], job_data['job_scale'],
                             job_data['job_salary_range'], job_data['job_skills'], today)
                    )
                except Exception as db_error:
                    logging.error(f"数据库操作失败: {str(db_error)}")
                finally:
                    db.close()

                time.sleep(10)
                browser.close()
                browser.switch_to.window(handle_main)

            except WebDriverException as e:
                logging.error(f"处理职位时发生浏览器错误: {str(e)}")
                browser.refresh()  # 刷新恢复状态
                time.sleep(10)
                continue

        return True
    except Exception as page_error:
        logging.error(f"处理第 {page_num} 页时发生错误: {str(page_error)}")
        return False

# 主循环
for i in range(3,23):
    success = False
    retry_count = 0
    while retry_count < 3 and not success:
        success = process_page(i)
        if not success:
            retry_count += 1
            time.sleep(10 * retry_count)  # 指数退避

browser.quit()